//
// Created by Tengjun Gao on 11/21/21.
//
//给你一个正整数 n ，生成一个包含 1 到 n² 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
//
//
//
// 示例 1：
//
//
//输入：n = 3
//输出：[[1,2,3],[8,9,4],[7,6,5]]
//
//
// 示例 2：
//
//
//输入：n = 1
//输出：[[1]]
//
//
//
//
// 提示：
//
//
// 1 <= n <= 20
//
// Related Topics 数组 矩阵 模拟 👍 527 👎 0
#include <vector>

using namespace std;

#include "iostream"

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private:
    void printMatrix(vector<vector<int>> answer) {
        for (vector<int> row: answer) {
            printf("[ ");
            for (int e: row) {
                printf("%5d ", e);
            }
            printf(" ]\n");
        }
        printf("=============================\n");
    }

public:
    vector<vector<int>> generateMatrix(int n) {
        printf("n=%d\n", n);
        vector<vector<int>> answer(n, vector<int>(n, 0)); // empty matrix with 0 filled
        //纯考代码熟练度
        int startx = 0, starty = 0; // 定义每循环一个圈的起始位置
        int loop = n / 2; // 每个圈循环几次， n=3, loop=1， 只循环一圈，矩阵中间的值需要单独处理
        int mid = n / 2; // 矩阵中间的位置， n=3, 中间的位置就是(1,1), n为5， 中间位置（2，2）
        int count = 1; // 用来给矩阵中每一个空格赋值
        int offset = 1; // 每一圈循环，需要控制每一条边遍历的长度
        int i, j;

        while (loop--) {
            printf("loop: %d\n", loop);

            i = startx;
            j = starty;

            // 下面4个 for 就是模拟转了一圈
            printf("startx = %d, starty = %d\n", startx, starty);
            // 模拟填充上行从左到右 (左闭右开）
            printf("1st row left to right:\n");
            for (j = starty; j < starty + n - offset; j++) { // 1-5 left->right (1st loop)
                answer[startx][j] = count++; // 先被赋值为count，然后count++
                printMatrix(answer);
            }

            // 模拟填充右列从上到下（左闭右开）
            printf("last col up to down:\n");
            for (i = startx; i < startx + n - offset; i++) { // 6-10 up->down (1st loop)
                answer[i][j] = count++;
                printMatrix(answer);
            }

            // 模拟填充下行从右到左（左闭右开）
            printf("last row right to left:\n");
            for (; j > starty; j--) { // 11-15 right->left (1st loop)
                answer[i][j] = count++;
                printMatrix(answer);
            }

            // 模拟填充左列从下到上（左闭右开）
            printf("first col down to up:\n");
            for (; i > startx; i--) { // 17-20 down->up (1st loop)
                answer[i][j] = count++;
                printMatrix(answer);
            }

            // 第二圈开始的时候， 起始位置要各自加1， eg 第一圈起始位置（0，0），第二圈起始位置为（1，1）
            printf("loop end, go to inner circle\n");
            startx++; // change start index x coordinate
            starty++; // change start index y coordinate
            printf("start point are going inner diagonally\n");

            // offset 控制每一圈每一条边遍历的长度
            offset += 2;

        }

        // 如果n为奇数， 需要单独给矩阵最中间的位置赋值
        if (n % 2) {
            answer[mid][mid] = count;
            printMatrix(answer);
        }

        printMatrix(answer);
        return answer;
    }
};

//leetcode submit region end(Prohibit modification and deletion)
int main() {
    int n = 6;
    Solution *s = new Solution();
    vector<vector<int>> answer = s->generateMatrix(n);
}

/*n=6 example
 * n=6
loop: 2
startx = 0, starty = 0
1st row left to right:
[     1     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     5     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
last col up to down:
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0     0  ]
[     0     0     0     0     0     0  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0     0     0     0     0     0  ]
=============================
last row right to left:
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0     0     0     0     0    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0     0     0     0    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0     0     0    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0     0    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[     0    15    14    13    12    11  ]
=============================
first col down to up:
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[     0     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[     0     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[     0     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[     0     0     0     0     0     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20     0     0     0     0     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
loop end, go to inner circle
start point are going inner diagonally
loop: 1
startx = 1, starty = 1
1st row left to right:
[     1     2     3     4     5     6  ]
[    20    21     0     0     0     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22     0     0     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23     0     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
last col up to down:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0     0     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0     0     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0    26     9  ]
[    17     0     0     0     0    10  ]
[    16    15    14    13    12    11  ]
=============================
last row right to left:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0    26     9  ]
[    17     0     0     0    27    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0    26     9  ]
[    17     0     0    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0    26     9  ]
[    17     0    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
first col down to up:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18     0     0     0    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19     0     0     0    25     8  ]
[    18    31     0     0    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32     0     0    25     8  ]
[    18    31     0     0    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
loop end, go to inner circle
start point are going inner diagonally
loop: 0
startx = 2, starty = 2
1st row left to right:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32    33     0    25     8  ]
[    18    31     0     0    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
last col up to down:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32    33    34    25     8  ]
[    18    31     0     0    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
last row right to left:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32    33    34    25     8  ]
[    18    31     0    35    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
first col down to up:
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32    33    34    25     8  ]
[    18    31    36    35    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================
loop end, go to inner circle
start point are going inner diagonally
[     1     2     3     4     5     6  ]
[    20    21    22    23    24     7  ]
[    19    32    33    34    25     8  ]
[    18    31    36    35    26     9  ]
[    17    30    29    28    27    10  ]
[    16    15    14    13    12    11  ]
=============================

Process finished with exit code 0*/